package com.hupu.jrs.injector.module

import android.app.NotificationManager
import android.content.Context
import android.view.LayoutInflater
import com.hupu.jrs.components.okhttp.CookieInterceptor
import com.hupu.jrs.components.okhttp.HttpLoggingInterceptor
import com.hupu.jrs.components.okhttp.OkHttpHelper
import com.hupu.jrs.components.retrofit.RequestHelper
import com.hupu.jrs.components.storage.UserStorage
import com.squareup.otto.Bus
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton
import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient

/**
 * @author gzsll
 */
@Module
class ApplicationModule(private val context: Context) {

    @Provides
    @Singleton
    fun provideApplicationContext(): Context {
        return context.applicationContext
    }

    @Provides
    @Singleton
    fun provideBusEvent(): Bus {
        return Bus()
    }

    @Provides
    @Singleton
    @Named("api")
    internal fun provideApiOkHttpClient(
            mCookieInterceptor: CookieInterceptor): OkHttpClient {
        val builder = OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
        val logging = HttpLoggingInterceptor()
        logging.setLevel(HttpLoggingInterceptor.Level.BODY)
        builder.addInterceptor(logging)
        builder.addInterceptor(mCookieInterceptor)
        return builder.build()
    }

    @Provides
    @Singleton
    internal fun provideOkHttpClient(@Named("api") mOkHttpClient: OkHttpClient): OkHttpClient {
        val builder = mOkHttpClient.newBuilder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
        builder.interceptors().clear()
        return builder.build()
    }

    @Provides
    @Singleton
    internal fun provideLayoutInflater(context: Context): LayoutInflater {
        return context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }

    @Provides
    @Singleton
    internal fun provideNotificationManager(mContext: Context): NotificationManager {
        return mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    @Provides
    @Singleton
    internal fun provideCookieInterceptor(mUserStorage: UserStorage): CookieInterceptor {
        return CookieInterceptor(mUserStorage)
    }

    @Provides
    @Singleton
    internal fun provideUserStorage(mContext: Context): UserStorage {
        return UserStorage(mContext)
    }

    @Provides
    @Singleton
    internal fun provideRequestHelper(mContext: Context,
                                      mUserStorage: UserStorage): RequestHelper {
        return RequestHelper(mContext, mUserStorage)
    }

    @Provides
    @Singleton
    internal fun provideOkHttpHelper(mOkHttpClient: OkHttpClient): OkHttpHelper {
        return OkHttpHelper(mOkHttpClient)
    }
}
